<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-header">
        <span>欢迎回来！</span>
      </div>
      <el-form :model="loginForm" ref="loginForm" :rules="rules" status-icon class="login-form">
        <el-form-item label="" prop="userName">
          <el-input v-model="loginForm.userName" :placeholder="loginForm.placeholderName" auto-complete="off" class="login-input"></el-input>
        </el-form-item>
        <el-form-item label="" prop="userPass">
          <el-input type="password" v-model="loginForm.userPass" :placeholder="loginForm.placeholderPass" auto-complete="off" class="login-input"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="userLogin('loginForm')" class="login-btn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { userLogin } from '../../api/login.js'
export default {
  name: 'Login',
  data() {
    const validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validateUserPass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('密码不能小于5位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        placeholderName: '用户名',
        placeholderPass: '密码',
        userName: '',
        userPass: ''
      },
      rules: {
        userName: [
          { required: true, validator: validateUserName, trigger: 'blur' }
        ],
        userPass: [
          { required: true, validator: validateUserPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    userLogin(loginForm) {
      this.$refs[loginForm].validate(valid => {
        if (valid) {
          const name = this.loginForm.userName
          const pass = this.loginForm.userPass
          const router = this.$router
          const route = this.$route
          userLogin({ uname: name, upsd: pass })
            .then(res => {
              const code = res.code
              const data = res.data
              if (code === '0') {
                const uadmin = data.uadmin
                switch (uadmin) {
                  case 0:
                    sessionStorage.setItem('user_token', 'admin')
                    break
                  case 1:
                    sessionStorage.setItem('user_token', 'other')
                    break
                }
                const USER_TOKEN = sessionStorage.getItem('user_token')
                if (USER_TOKEN != null) {
                  router.push(route.query.redirect || '/')
                }
              }
            })
            .catch(error => {
              console.log(error)
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.login-container {
  width: 100%;
  height: 100%;
  background-image: url(../../assets/login_bg.png);
  background-size: cover;
  background-position: 50%;
  position: relative;
  .login-card {
    box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.8);
    width: 300px;
    position: absolute;
    top: 30%;
    right: 360px;
    background: rgba(0, 0, 0, 0.5);
    border: none;

    .login-header {
      padding: 5px 10px;
      margin-bottom: 15px;
      text-align: center;
      color: #fff;
    }
    .login-input {
      box-shadow: 1px 3px 5px rgba(0, 0, 0, 0.5);
    }
    .login-btn {
      width: 100%;
      box-shadow: 1px 3px 5px rgba(0, 0, 0, 0.5);
    }
  }
}
</style>